{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a9fc53da",
   "metadata": {},
   "source": [
    "# Add Tensors\n",
    "\n",
    "The following simple examples demonstrate how to create two\n",
    "tensors and add them together. TT-NN is a high-level Python API\n",
    "designed for developers to run models like LLaMA, Mistral, Stable\n",
    "Diffusion, and more. For a full list of models see the [model matrix](https://github.com/tenstorrent/tt-metal/blob/main/models/README.md).\n",
    "\n",
    "Lets create the example file, `ttnn_add_tensors.py`\n",
    "\n",
    "## Import Libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf748f2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ttnn\n",
    "from loguru import logger"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98fa077a",
   "metadata": {},
   "source": [
    "## Open Tenstorrent device\n",
    "\n",
    "We will create a device to run the program."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c57695f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Open the Device\n",
    "device = ttnn.open_device(device_id=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e77121e8",
   "metadata": {},
   "source": [
    "## Tensor Creation\n",
    "\n",
    "Create two TT-NN tensors, and initialize them with values 1 and 2\n",
    "respectively. The preferred tensor shape is (32, 32) which\n",
    "matches the hardware tile size. Learn more about tensors [here](https://github.com/tenstorrent/tt-metal/blob/main/docs/source/ttnn/ttnn/tensor.rst)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cbb0e5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create two TT-NN tensors with TILE_LAYOUT\n",
    "tt_tensor1 = ttnn.full(\n",
    "    shape=(32, 32),\n",
    "    fill_value=1.0,\n",
    "    dtype=ttnn.float32,\n",
    "    layout=ttnn.TILE_LAYOUT,\n",
    "    device=device,\n",
    ")\n",
    "tt_tensor2 = ttnn.full(\n",
    "    shape=(32, 32),\n",
    "    fill_value=2.0,\n",
    "    dtype=ttnn.float32,\n",
    "    layout=ttnn.TILE_LAYOUT,\n",
    "    device=device,\n",
    ")\n",
    "logger.info(\"Input tensors:\")\n",
    "logger.info(tt_tensor1)\n",
    "logger.info(tt_tensor2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f49cd16d",
   "metadata": {},
   "source": [
    "## Addition Operation and Conversion\n",
    "\n",
    "Now we can perform the addition operation on the two TT-NN tensors and\n",
    "log out the result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "063e9482",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Perform eltwise addition on the device\n",
    "tt_result = ttnn.add(tt_tensor1, tt_tensor2)\n",
    "\n",
    "# Log output tensor\n",
    "logger.info(\"Output tensor:\")\n",
    "logger.info(tt_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4703a9a",
   "metadata": {},
   "source": [
    "## Close the Device"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29a8e1ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "ttnn.close_device(device)\n",
    "logger.info(\"Device closed successfully.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3e11550",
   "metadata": {},
   "source": [
    "## Full Example and Output\n",
    "\n",
    "Combine all previous examples for a complete example that can be run\n",
    "directly. This example opens a Tenstorrent device, creates two\n",
    "tensors, performs the addition, and logs the output tensor.\n",
    "\n",
    "[ttnn_add_tensors.py](https://github.com/tenstorrent/tt-metal/tree/main/ttnn/tutorials/basic_python/ttnn_add_tensors.py)\n",
    "\n",
    "Running this script will output the input tensors and the result of\n",
    "their addition, which should be a tensor filled with 3s. As shown below\n",
    "\n",
    "``` console\n",
    "$ python3 $TT_METAL_HOME/ttnn/tutorials/basic_python/ttnn_add_tensors.py\n",
    "2025-06-23 09:36:58.211 | INFO     | __main__:main:29 - Input tensors:\n",
    "2025-06-23 09:36:58.211 | INFO     | __main__:main:30 - ttnn.Tensor([[ 1.00000,  1.00000,  ...,  1.00000,  1.00000],\n",
    "            [ 1.00000,  1.00000,  ...,  1.00000,  1.00000],\n",
    "            ...,\n",
    "            [ 1.00000,  1.00000,  ...,  1.00000,  1.00000],\n",
    "            [ 1.00000,  1.00000,  ...,  1.00000,  1.00000]], shape=Shape([32, 32]), dtype=DataType::FLOAT32, layout=Layout::TILE)\n",
    "2025-06-23 09:36:58.211 | INFO     | __main__:main:31 - ttnn.Tensor([[ 2.00000,  2.00000,  ...,  2.00000,  2.00000],\n",
    "            [ 2.00000,  2.00000,  ...,  2.00000,  2.00000],\n",
    "            ...,\n",
    "            [ 2.00000,  2.00000,  ...,  2.00000,  2.00000],\n",
    "            [ 2.00000,  2.00000,  ...,  2.00000,  2.00000]], shape=Shape([32, 32]), dtype=DataType::FLOAT32, layout=Layout::TILE)\n",
    "2025-06-23 09:37:00.524 | INFO     | __main__:main:37 - Output tensor:\n",
    "2025-06-23 09:37:00.525 | INFO     | __main__:main:38 - ttnn.Tensor([[ 3.00000,  3.00000,  ...,  3.00000,  3.00000],\n",
    "            [ 3.00000,  3.00000,  ...,  3.00000,  3.00000],\n",
    "            ...,\n",
    "            [ 3.00000,  3.00000,  ...,  3.00000,  3.00000],\n",
    "            [ 3.00000,  3.00000,  ...,  3.00000,  3.00000]], shape=Shape([32, 32]), dtype=DataType::FLOAT32, layout=Layout::TILE)\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
